<?php

/**
 * @copyright   FOSP
 * @version     20120412
 * @todo        Mysql access to table player
 * @name        Fpt_Business_Player_Adapter_Mysql
 * @author      QuangTM 
 */
class Fpt_Business_Player_Adapter_Mysql extends Fpt_Business_Player_Adapter_Abstract
{

    /**
     * Get Fpt_Business_Player_Adapter_Mysql's instance
     * @return Fpt_Business_Player_Adapter_Mysql
     * @author QuangTM
     */
    public static function getInstance()
    {
        if (!isset(self::$instance) || !self::$instance instanceof self)
            self::$instance = new self;
        return self::$instance;
    }

    public function getBasicData($arrPlayerIDs)
    {
        try
        {
            if (!is_array($arrPlayerIDs) || !count($arrPlayerIDs))
            {
                Fpt_Log::sendlog(4, 1, 'Invalid params', NULL, NULL, NULL);
                return FALSE;
            }
            $strPlayerIDs = implode(', ', $arrPlayerIDs);
            // Get DB Obj
            $dbObj = Fpt_Global::getSportSlaveDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_fesport_getDetailPlayers(:p_playerids);');

            // Bind param
            $stmt->bindParam('p_playerids', $strPlayerIDs, PDO::PARAM_STR);

            // Execute
            $stmt->execute();

            // Get result
            $result = array();
            
            // Get format instance
            $formatInstance = new Fpt_Business_Player_Metadata_Player();
            
            // Loop to get data
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
            {
                $result[$row['player_id']] = $formatInstance->init($row)->getBasicData();
            }

            // Close cursor
            $stmt->closeCursor();

            // Release
            unset($stmt, $formatInstance);

            return $result;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    public function getPlayerByTeamId($teamId)
    {
        try {
            // Get DB Obj
            $dbObj = Fpt_Global::getSportSlaveDB();
            
            // Prepare sql
            $stmt = $dbObj->prepare('Call sp_sport_getListPlayersByTeam(:p_teamid);');

            // bind params
            $stmt->bindParam('p_teamid', $teamId, PDO::PARAM_INT);

            // execute
            $stmt->execute();

            //Fetch Result
            $result = $stmt->fetchAll();

            // Close cursor
            $stmt->closeCursor();

            // Release
            unset($stmt);

            //Return array
            return $result;
            
        } catch (Exception $ex) {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    public function getDetail($playerId)
    {
        try {
            // Get DB Obj
            $dbObj = Fpt_Global::getSportSlaveDB();

            $stmt = $dbObj->prepare('Call sp_besport_getDetailPlayers(:p_playerid);');
            // bind params
            $stmt->bindParam('p_playerid', $playerId, PDO::PARAM_INT);

            // execute
            $stmt->execute();

            //Fetch Result
            $result = $stmt->fetch();

            // Close cursor
            $stmt->closeCursor();

            // Release
            unset($stmt);

            //Return array
            return $result;

        } catch (Exception $ex) {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

}